package top.whysu.rongcloud.controller;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.whysu.rongcloud.dto.http.Response;
import top.whysu.rongcloud.dto.rongcloud.groups.*;
import top.whysu.rongcloud.entity.validate.rongcloud.GroupsBulletinValid;
import top.whysu.rongcloud.entity.validate.rongcloud.GroupsNameValid;
import top.whysu.rongcloud.entity.validate.rongcloud.GroupsPortraitValid;
import top.whysu.rongcloud.service.RongCloudGroupsService;
import top.whysu.rongcloud.util.ResponseUtil;

import javax.annotation.Resource;
import javax.validation.groups.Default;

/**
 * 群组（本地维护 群组关系表）
 */
@RestController
@RequestMapping("/rongCloud/groups")
public class GroupsController {
    @Resource
    RongCloudGroupsService rongCloudGroupsService;

    /**
     * 创建群组
     */
    @PostMapping("/create")
    public Response create(@RequestBody @Validated GroupsCreateDTO groupsCreateDTO) {
        return ResponseUtil.getSuccessResponse(rongCloudGroupsService.wholeCreateGroups(groupsCreateDTO));
    }

    /**
     * 添加群成员
     */
    @PostMapping("/add")
    public Response add(@RequestBody @Validated GroupsAddDTO groupsAddDTO) {
        rongCloudGroupsService.wholeAdd(groupsAddDTO);
        return ResponseUtil.getSuccessResponse();
    }

    /**
     * 用户加入群组
     */
    @PostMapping("/join")
    public Response join(@RequestBody @Validated GroupsJoinDTO groupsJoinDTO) {
        rongCloudGroupsService.wholeJoin(groupsJoinDTO);
        return ResponseUtil.getSuccessResponse();
    }


    /**
     * 群主或群管理将群成员移出群组
     */
    @PostMapping("/kick")
    public Response kick(@RequestBody @Validated GroupsKickDTO groupsKickDTO) {
        rongCloudGroupsService.wholeKick(groupsKickDTO);
        return ResponseUtil.getSuccessResponse();
    }

    /**
     * 退出群组
     */
    @PostMapping("/quit")
    public Response quit(@RequestBody GroupsDTO groupsDTO) {
        rongCloudGroupsService.wholeQuit(groupsDTO);
        return ResponseUtil.getSuccessResponse();
    }

    /**
     * 群组重命名
     */
    @PostMapping("/rename")
    public Response rename(@RequestBody @Validated({Default.class, GroupsNameValid.class}) GroupsModifyDTO groupsModifyDTO) {
        rongCloudGroupsService.wholeModifyGroups(groupsModifyDTO);
        return ResponseUtil.getSuccessResponse();
    }

    /**
     * 修改群公告
     */
    @PostMapping("/set_bulletin")
    public Response setBulletin(@RequestBody @Validated({GroupsBulletinValid.class}) GroupsModifyDTO groupsModifyDTO) {
        rongCloudGroupsService.wholeModifyGroups(groupsModifyDTO);
        return ResponseUtil.getSuccessResponse();
    }

    /**
     * 修改群头像
     */
    @PostMapping("/set_portrait_uri")
    public Response setPortraitUri(@RequestBody @Validated({GroupsPortraitValid.class}) GroupsModifyDTO groupsModifyDTO) {
        rongCloudGroupsService.wholeModifyGroups(groupsModifyDTO);
        return ResponseUtil.getSuccessResponse();
    }

    /**
     * 设置群名片
     */
    @PostMapping("/set_display_name")
    public Response setDisplayName(@RequestBody @Validated GroupsDisplayNameDTO groupsDisplayNameDTO) {
        rongCloudGroupsService.wholeModifyDisplayName(groupsDisplayNameDTO);
        return ResponseUtil.getSuccessResponse();
    }

    /**
     * 获取用户id的所有群组
     */
    @GetMapping("/getGroups")
    public Response getGroups(@RequestParam @Validated GroupsUserAccountDTO groupsUserAccountDTO) {
        return ResponseUtil.getSuccessResponse(rongCloudGroupsService.getGroupList(groupsUserAccountDTO.getUserAccount()));
    }

    /**
     * 获取群组的所有成员
     */
    @GetMapping("/getMembers")
    public Response getMembers(@RequestParam @Validated GroupsGroupIdDTO groupsGroupIdDTO) {
        return ResponseUtil.getSuccessResponse(rongCloudGroupsService.getMemberList(groupsGroupIdDTO.getGroupId()));
    }

    /**
     * 解散群组
     * userId:  必需是群主才能解散该群
     * groupId:  群id
     */
    @PostMapping("/dismiss")
    public Response dismiss(@RequestBody @Validated GroupsDTO groupsDTO) {
        rongCloudGroupsService.wholeDismiss(groupsDTO.getUserAccount(), groupsDTO.getGroupId());
        return ResponseUtil.getSuccessResponse();
    }

    /**
     * 根据群组id获取群组信息
     */
    @GetMapping("/getGroupsInfo")
    public Response getGroupsInfo(@RequestParam @Validated GroupsGroupIdDTO groupsGroupIdDTO) {
        return ResponseUtil.getSuccessResponse(rongCloudGroupsService.selectGroupInfoById(groupsGroupIdDTO.getGroupId()));
    }

}
